﻿/****************************************************************
 * Copyright (c) 2010-2013 Besture All Rights Reserved.
 * 
 * File Name:		UserService 
 * 
 * Created:		2013-2-1 14:13:47
 * Original Author:	nitao 
 * 
 * ------------------------------------------------------------
 * MODIFICATION HISTORY
 * ------------------------------------------------------------
 * When			Who		Why
 * ------------------------------------------------------------
 * 
 *
 ****************************************************************/

using System.Data;
using System.Web;
using Besture.Model;

namespace Besture.Bll
{
    public class AdminUserService
    {
        /// <summary>
        /// 用户名称
        /// </summary>
        public const string  SESSION_CURRENT_ADMIN_USER_LOGINNAME = "CurrentAdminUserName";
        /// <summary>
        /// 用户模型
        /// </summary>
        public const string SESSION_CURRENT_ADMIN_USER_LOGINNAME_MODEL = "CurrentAdminUserModel";

        public const string SESSION_CURRENT_ADMIN_USER_CULTUREINFO = "CultureInfo";

        /// <summary>
        /// 当前管理用户
        /// </summary>
        /*public static AdminUserModel AdminLoginUser
        {
            get
            {
                if (HttpContext.Current.Session[SESSION_CURRENT_ADMIN_USER_LOGINNAME_MODEL] != null)
                {
                    return (AdminUserModel)HttpContext.Current.Session[SESSION_CURRENT_ADMIN_USER_LOGINNAME_MODEL];
                }
                else
                {
                    HttpCookie userNameCookie = HttpContext.Current.Request.Cookies["CtrlCRMLoginUserName"];
                    HttpCookie passwordCookie = HttpContext.Current.Request.Cookies["CtrlCRMPassword"];
                    if (userNameCookie != null && userNameCookie.Value != null &&
                        passwordCookie != null && passwordCookie.Value != null)
                    {
                        AdminUserModel userModel = new AdminUserDac().GetModel(userNameCookie.Value);
                        if (userModel != null && userModel.Password == passwordCookie.Value)
                        {
                            HttpContext.Current.Session[SESSION_CURRENT_ADMIN_USER_LOGINNAME_MODEL] = userModel;
                            return userModel;
                        }
                        else
                        {
                            return null;
                        }
                    }
                    else
                    {
                        return null;
                    }
                }
            }
        }*/

        /// <summary>
        /// 当前管理用户
        /// </summary>
        public static AdminUserModel AdminLoginUser
        {
            get
            {
                if (HttpContext.Current.Session == null || HttpContext.Current.Session[SESSION_CURRENT_ADMIN_USER_LOGINNAME_MODEL] == null)
                {
                    return new AdminUserModel();
                }
                //AdminUserModel adminUserModel = new AdminUserDac().GetModel("admin");
                AdminUserModel adminUserModel = HttpContext.Current.Session[SESSION_CURRENT_ADMIN_USER_LOGINNAME_MODEL] as AdminUserModel;
                return adminUserModel;
            }
        }

        protected AdminUserModel LoginUserInfo
        {
            get
            {
                if (HttpContext.Current.Session[SESSION_CURRENT_ADMIN_USER_LOGINNAME_MODEL] != null)
                {
                    return (AdminUserModel)HttpContext.Current.Session[SESSION_CURRENT_ADMIN_USER_LOGINNAME_MODEL];
                }
                else
                {
                    HttpCookie userNameCookie = HttpContext.Current.Request.Cookies["CtrlCRMLoginUserName"];
                    HttpCookie passwordCookie = HttpContext.Current.Request.Cookies["CtrlCRMPassword"];
                    if (userNameCookie != null && userNameCookie.Value != null &&
                        passwordCookie != null && passwordCookie.Value != null)
                    {
                        AdminUserModel userModel = new AdminUserDac().GetModel(userNameCookie.Value);
                        if (userModel != null && userModel.Password == passwordCookie.Value)
                        {
                            HttpContext.Current.Session[SESSION_CURRENT_ADMIN_USER_LOGINNAME_MODEL] = userModel;
                            return userModel;
                        }
                        else
                        {
                            return null;
                        }
                    }
                    else
                    {
                        return null;
                    }
                }
            }
        }


        public static DataTable GetPageTableByModulesId(string modulesId)
        {
            DataTable table = AdminLoginUser.tablePagePermissions;
            if(table == null)
            {
                return null;
            }
            DataView view =  table.DefaultView;
            view.RowFilter = "ModulesID=" + modulesId;
            return view.Table;
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="loginName"></param>
        public static void Login(string loginName)
        {
            AdminUserModel adminUserModel = new AdminUserDac().GetModel(loginName);
            HttpContext.Current.Session[SESSION_CURRENT_ADMIN_USER_LOGINNAME] = loginName;
            HttpContext.Current.Session[SESSION_CURRENT_ADMIN_USER_LOGINNAME_MODEL] = adminUserModel;
        }

        /// <summary>
        /// 退出
        /// </summary>
        public static void Out()
        {
            HttpContext.Current.Session[SESSION_CURRENT_ADMIN_USER_LOGINNAME] = null;
            HttpContext.Current.Session[SESSION_CURRENT_ADMIN_USER_LOGINNAME_MODEL] = null;
        }
    }
}
